package com.ly.mp.busicen.common.context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.springframework.util.StringUtils;

import com.ly.mp.busicen.common.constant.UserBusiEntity;
import com.ly.mp.busicen.common.helper.SessionHelper;
import com.ly.mp.component.entities.UserEntity;

public class BusicenContext {

    public static final String BUSICEN_QUERY_ORDERS = "busicen_query_orders";
    public static final String BUSICEN_ERRORS = "busicen_errors";
    public static final String BUSICEN_RBG = "busicen_rbg";

    private static List<Consumer<String>> initCallBacks = new ArrayList<>();

    private static List<Consumer<String>> releaseCallBacks = new ArrayList<>();

    private static final ThreadLocal<Map<String, Object>> CONTEXT = new ThreadLocal<Map<String, Object>>();

    private static final ThreadLocal<String> TOKEN = new ThreadLocal<String>();

    private static final ThreadLocal<UserEntity> CURRENTUSER = new ThreadLocal<>();

    static {
        addInitCallBack(tk -> {
            UserEntity userEntity = (UserEntity) SessionHelper.get(tk);
            CURRENTUSER.set(userEntity);
        });
    }

    public static void addInitCallBack(Consumer<String> consumer) {
        if (!initCallBacks.contains(consumer)) {
            initCallBacks.add(consumer);
        }
    }

    public static void addReleaseCallBack(Consumer<String> consumer) {
        if (!releaseCallBacks.contains(consumer)) {
            releaseCallBacks.add(consumer);
        }
    }

    public static UserEntity getCurrentUser() {
        return CURRENTUSER.get();
    }

    public static UserBusiEntity getCurrentUserBusiInfo() {
        UserEntity busiUser = getCurrentUser();
        return getCurrentUserBusiInfo(busiUser);
    }

    public static UserBusiEntity getCurrentUserBusiInfo(UserEntity busiUser) {
        UserBusiEntity busiUserEntity = new UserBusiEntity();
        if (busiUser != null) {
            busiUserEntity.setUserID(busiUser.getUserID());
            busiUserEntity.setUserName(busiUser.getUserName());
            busiUserEntity.setEmpID(busiUser.getEmpID());
            busiUserEntity.setEmpName(busiUser.getEmpName());
            busiUserEntity.setDlrCode(busiUser.getExt("DLR_CODE"));
            busiUserEntity.setDlrName(busiUser.getExt("DLR_NAME"));
            busiUserEntity.setDlrID(busiUser.getExt("DLR_ID"));
            busiUserEntity.setParentDlrCode(busiUser.getExt("PARENT_DLR_CODE"));
            busiUserEntity.setParentDlrName(busiUser.getExt("PARENT_DLR_NAME"));
            busiUserEntity.setParentDlrID(busiUser.getExt("PARENT_DLR_ID"));
            busiUserEntity.setDlrOrgType(busiUser.getExt("DLR_ORG_TYPE"));
            busiUserEntity.setDlrStatus(busiUser.getExt("DLR_STATUS"));
            busiUserEntity.setOrgType(busiUser.getExt("ORG_TYPE"));
            busiUserEntity.setOrgCode(busiUser.getExt("ORG_CODE"));
            busiUserEntity.setOrgID(busiUser.getExt("ORG_ID"));
            busiUserEntity.setOrgName(busiUser.getExt("ORG_NAME"));
            busiUserEntity.setPosID(busiUser.getExt("POS_ID"));
            busiUserEntity.setPosCode(busiUser.getExt("POS_CODE"));
            busiUserEntity.setBelongFactoryId(busiUser.getExt("BELONG_FACTORY_ID"));
            busiUserEntity.setBelongFactoryCode(busiUser.getExt("BELONG_FACTORY_CODE"));
            busiUserEntity.setSolutionId(busiUser.getExt("SOLUTION_ID"));
            busiUserEntity.setCompanyID(busiUser.getExt("COMPANY_ID"));
        } else {
            return busiUserEntity;
        }
        if (!StringUtils.hasLength(busiUser.getExt("GROUP_ID"))) {
            busiUserEntity.setGroupID("1");
        } else {
            busiUserEntity.setGroupID(busiUser.getExt("GROUP_ID"));
        }
        if (!StringUtils.hasLength(busiUser.getExt("GROUP_CODE"))) {
            busiUserEntity.setGroupCode("1");
        } else {
            busiUserEntity.setGroupCode(busiUser.getExt("GROUP_CODE"));
        }
        if (!StringUtils.hasLength(busiUser.getExt("OEM_ID"))) {
            busiUserEntity.setOemID("1");
        } else {
            busiUserEntity.setOemID(busiUser.getExt("OEM_ID"));
        }
        if (!StringUtils.hasLength(busiUser.getExt("OEM_CODE"))) {
            busiUserEntity.setOemCode("1");
        } else {
            busiUserEntity.setOemCode(busiUser.getExt("OEM_CODE"));
        }
        if (!StringUtils.hasLength(busiUser.getExt("BRAND_CODE"))) {
            busiUserEntity.setBrandCode("1");
        } else {
            busiUserEntity.setBrandCode(busiUser.getExt("BRAND_CODE"));
        }

        return busiUserEntity;
    }

    public static void setToken(String token) {
        TOKEN.set(token);//设置token
        //执行初始化回调
        if (initCallBacks != null) {
            initCallBacks.forEach(m -> {
                m.accept(token);
            });
        }
    }

    public static String getToken() {
        return TOKEN.get();
    }

    public static Map<String, Object> getContext() {
        return CONTEXT.get();
    }

    public static void setContext(Map<String, Object> context) {
        CONTEXT.set(context);
    }

    public static void remove() {
        if (releaseCallBacks != null) {
            releaseCallBacks.forEach(m -> {
                m.accept(getToken());
            });
        }

        CONTEXT.remove();
        TOKEN.remove();
        CURRENTUSER.remove();
    }

}
